package pers.lt.tools;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.Properties;

/**
 * liting
 *
 * **/
public class StringFacs {
    private static long id = 1L;
    public static boolean isEmpty(Object baseObject){
        if (baseObject==null || baseObject.toString().equals("")) return true;
        else return false;
    }


    public static String getGuid32(){
        return RandomStrg.getGUID32();
    }
    //
    public static String completionZero(String paCode, int paLong){
        if(!StringFacs.isEmpty(paCode)){
            int codeLength = paCode.length();
            if(codeLength<paLong){
                for(int i = 0;i<(paLong-codeLength);i++){
                    paCode = 0+paCode;

                }

            }
        }
        return paCode;
    }

    //oracle.sql.Clob类型转换成String类型

    public static String ClobToString(Clob clob) throws SQLException, IOException {

        String reString = "";
        Reader is = clob.getCharacterStream();// 得到流
        BufferedReader br = new BufferedReader(is);
        String s = br.readLine();
        StringBuffer sb = new StringBuffer();
        while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
            sb.append(s);
            s = br.readLine();
        }
        reString = sb.toString();
        return reString;
    }
    //取文件中的properties 属性
    public static String getXmlProperties(String paResourceName, String paPerpertiesName) {
        Resource resource = null;
        Properties props = null;
        String driverClass = null;
        try {
            //sqlconfig.properties
            resource = new ClassPathResource("/"+paResourceName);
            props = PropertiesLoaderUtils.loadProperties(resource);
            driverClass = (String) props.get(paPerpertiesName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return driverClass;
    }
    public static String getClassRoot(HttpServletRequest request){
        return  request.getSession().getServletContext().getRealPath("")+"\\WEB-INF\\classes\\";

    }
    public static String getWebRoot(HttpServletRequest request){
        return  request.getSession().getServletContext().getRealPath("");

    }
    //生成20位唯一编码
    public static String getUUID(){
        return   RandomStrg.getGUID32();
    }
    public static String decimalFormat(String paNumber, String paFormat){
        DecimalFormat decimalFormat = new DecimalFormat(paFormat);
        String rs = decimalFormat.format(Float.parseFloat(paNumber));
        String[] lar_1 = rs.split("\\.");
        String[] lar_2 = paFormat.split("\\.");
        int alength = lar_2[1].length();
        if(lar_1.length>1){
            int blength = lar_1[1].length();
            for(int i=0;i<(alength-blength);i++){
                rs+=0;
            }

        }else{
            rs+=".";
            for(int i=0;i<alength;i++){
                rs+=0;
            }

        }
        return rs;

    }

    public static void main(String[] args) {
        // System.out.println(StringFacs.decimalFormat("3.","#.###"));
    }
}